package com.shang.dao.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.shang.dao.CommentDao;
import com.shang.dto.CommentDTO;
import com.shang.entity.Comment2;
import com.shang.entity.UserMark;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CommentDaoImpl implements CommentDao {

    private  String collectionName="comment2";

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public int getCommentValue(long comment_id) {
        Criteria criteria=Criteria.where("comment_id").is(comment_id);
        Query query=new Query(criteria);
        Comment2 comment2 = mongoTemplate.findOne(query, Comment2.class, "comment2");
        return comment2.getScoreValue();
    }

    @Override
    public List<CommentDTO> getCommentsByPage(int pageNum, int pageSize) {
        List<CommentDTO> commentDTOList=new ArrayList<>();
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        Query query=new Query().with(pageable);
        List<Comment2> commentList = mongoTemplate.find(query, Comment2.class, collectionName);
        int size = commentList.size();
        //将Comment2对象 改换成 CommentDTO对象 主要是isMark类型 从int 变为boolean
        for (int i=0;i<size;i++){
            CommentDTO commentDTO=new CommentDTO();
            Comment2 comment2 = commentList.get(i);
            commentDTO.setComment_id(comment2.getComment_id());
            commentDTO.setUserName(comment2.getUserName());
            commentDTO.setData(comment2.getData());
            commentDTO.setEndorse_num(comment2.getEndorse_num());
            commentDTO.setScoreValue(comment2.getScoreValue());
            commentDTO.setContent(comment2.getContent());
            if (comment2.getIsMark()==0){
                commentDTO.setMark(false);
            }
            else {
                commentDTO.setMark(true);
            }
            commentDTOList.add(commentDTO);
        }
        return commentDTOList;
    }

    @Override
    public int getCommentNumber() {
        List<Comment2> comment2List = mongoTemplate.find(new Query(), Comment2.class, collectionName);
        return comment2List.size();
    }

    @Override
    public int getCommentValueById(long comment_id) {
        Criteria criteria=Criteria.where("comment_id").is(comment_id);
        Query query=new Query(criteria);
        Comment2 comment = mongoTemplate.findOne(query, Comment2.class, "comment2");
        int scoreValue = comment.getScoreValue();
        return scoreValue;
    }

    @Override
    public int updateCommentValue(long comment_id, int scoreValue) {
        Criteria criteria=Criteria.where("_id").is(comment_id);
        Query query=new Query(criteria);
        Update update=new Update().set("scoreValue",scoreValue).set("_id",comment_id);
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Comment2.class, collectionName);
        long modifiedCount = updateResult.getModifiedCount();
        int updateNum= ((int) modifiedCount);
        return updateNum;
    }

    @Override
    public int updateCommentMark(long comment_id, int isMark) {
        Criteria criteria=Criteria.where("_id").is(comment_id);
        Query query=new Query(criteria);
        Update update=new Update().set("isMark",isMark).set("_id",comment_id);
        UpdateResult updateResult=mongoTemplate.updateFirst(query,update,Comment2.class,collectionName);
        int updateNum = (int) updateResult.getModifiedCount();
        return updateNum;
    }

    @Override
    public int addUserMark(String userName, long comment_id,String markTime) {
        Criteria criteria = Criteria.where("comment_id").is(comment_id);
        Query query = new Query(criteria);
        Comment2 comment2 = mongoTemplate.findOne(query, Comment2.class, "comment2");
        UserMark userMark=new UserMark();
        userMark.setComment_user(comment2.getUserName());
        userMark.setUserName(userName);
        userMark.setComment_id(comment_id);
        userMark.setSolved(false);
        userMark.setTelephone("");
        userMark.setMarkTime(markTime);
        UserMark insertNum = mongoTemplate.insert(userMark,"user_mark");
        if (insertNum==null){
            return 0;
        }
        else {
            return 1;
        }
    }

    @Override
    public boolean checkMark(String userName, long comment_id) {
        Criteria criteria=Criteria.where("userName").is(userName).and("comment_id").is(comment_id);
        Query query=new Query(criteria);
        List<UserMark> checked = mongoTemplate.find(query, UserMark.class, "user_mark");
        if (checked.size()!=0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int removeMark(String userName, long comment_id) {
        Criteria criteria=Criteria.where("userName").is(userName).and("comment_id").is(comment_id);
        Query query=new Query(criteria);
        DeleteResult remove = mongoTemplate.remove(query, "user_mark");
        int deletedCount = (int) remove.getDeletedCount();
        return deletedCount;
    }

    @Override
    public int getUserMarkNum(String userName) {
        String collectionName="user_mark";
        Criteria criteria=Criteria.where("userName").is(userName);
        Query query=new Query(criteria);
        List<UserMark> userMarkList = mongoTemplate.find(query, UserMark.class, collectionName);
        int userMarkNum = userMarkList.size();
        return userMarkNum;
    }

    @Override
    public boolean haveScore(long comment_id) {
        Criteria criteria=Criteria.where("comment_id").is(comment_id);
        Query query=new Query(criteria);
        Comment2 comment2 = mongoTemplate.findOne(query, Comment2.class, "comment2");
        if (comment2.getScoreValue()==0){
            return false;
        }
        else{
            return true;
        }
    }
}
